<template>
	<view class="content">
		<u-sticky bgColor="#fff">
			<u-tabs :list="tabList" :current="tabActive" @click="change" lineWidth="61rpx"
				lineColor="rgba(243, 39, 28, 1)"></u-tabs>
		</u-sticky>
		<!-- 聊天消息 -->
		<view class="chatBox" v-if="tabActive==0">
			<view class="chatItemMessage" v-for="item in chatList" :key="item.id" @click="enterchat(item)">
				<view class="imageBox">
					<image :src="item.firend.avatar" mode="">
					</image>
				</view>
				<view class="right">
					<view class="nameTime">
						<view class="flexleft">
							<view class="name">{{item.firend.nickname}}</view>
							<view class="number" v-if="item.noread_num">{{item.noread_num}}</view>
						</view>
						<view class="time" v-if="item.new_content">{{item.new_content.createtime_text}}</view>
					</view>
					<template v-if="item.new_content">
						<template v-if="item.new_content.msg_status==1">
							<view class="bottom yihang" v-if="item.new_content.data_type==1">
								{{item.new_content.message}}</view>
							<view class="bottom yihang" v-if="item.new_content.data_type==2">[图片]</view>
							<view class="bottom yihang" v-if="item.new_content.data_type==3">[视频]</view>
							<view class="bottom yihang" v-if="item.new_content.data_type==4">[语音]</view>
						</template>
						<template v-if="item.new_content.msg_status==2">
							<view class="bottom yihang">{{item.new_content.sender_id=='user'+user_id?'你':'对方'}}撤回了一条消息
							</view>
						</template>
					</template>
				</view>
			</view>
			<view class="flexcenter placeholderBoxTop" v-if="!chatList.length">
				<image class="placeholderBox" src="/static/placeholderimg.png" mode=""></image>
			</view>
		</view>
		<!-- 互动消息 -->
		<view class="interactionBox" v-if="tabActive==1">
			<view class="interactionItem" v-for="item in list.data" :key="item.id" @click="goInfo(item)">
				<view class="left">
					<view class="imageBox" v-if="item.user">
						<image :src="item.user.avatar" mode="widthFix"></image>
					</view>
					<view>
						<view class="name" v-if="item.user">{{item.user.nickname}}</view>
						<view class="word_wrap" style="margin: 16rpx 0;">
							<view v-if="item.type==1">
								<u-icon name="share-square" color="rgba(235, 103, 103, 1)" size="22"></u-icon>
							</view>
							<view v-if="item.type==2">
								<u-icon name="heart" color="rgba(235, 103, 103, 1)" size="22"></u-icon>
							</view>
							<view class="reply" v-if="item.type==4">{{item.communityreply.content}}</view>
							<view class="reply" v-if="item.type==5">回复了
								<text style="color: rgba(243, 39, 28, 1);"
									v-if="item.communityreply.replyuser">{{item.communityreply.replyuser.nickname}}</text>
								{{item.communityreply.content}}
							</view>
						</view>
						<view class="time">{{item.createtime_text}}</view>
					</view>
				</view>
				<view class="flexcenter word_wrap" v-if="item.community">
					<view class="right" v-if="item.community.images_arr.length">
						<image :src="item.community.images_arr[0]" mode="widthFix"></image>
					</view>
					<view class="reply lianghang" v-if="!item.community.images_arr.length">
						{{item.community.title}}
					</view>
				</view>
			</view>
			<view class="flexcenter placeholderBoxTop" v-if="!list.data.length">
				<image class="placeholderBox" src="/static/placeholderimg.png" mode="widthFix"></image>
			</view>
		</view>
		<!-- 系统通知 -->
		<view class="interactionBox" v-if="tabActive==2">
			<view class="padding border_2 ffffff margin_bottom3" v-for="(item,index) in noticelist.data" :key="index">
				<view class="justify_between">
					<view class="color_ling text_x">{{item.name}}</view>
					<view class="color_six text_five textright" style="width: 50%">{{date(item.createtime)}}</view>
				</view>
				<view class="flex padding_bottom border_bottom margin_top">
					<view class="goodsImg margin_right2" v-if="item.image">
						<image :src="pubUrl+item.image" mode="widthFix"></image>
					</view>
					<view class="color_san text_a flex1">
						{{item.content}}
					</view>
				</view>
				<view class="margin_top text_a" style="color: #F3271C;" v-if="item.warning_num!=0">
					当前累计{{item.warning_num}}次</view>
			</view>
			<view class="flexcenter placeholderBoxTop" v-if="!noticelist.data.length">
				<image class="placeholderBox" src="/static/placeholderimg.png" mode="widthFix"></image>
			</view>
		</view>
		<tabbardi :current="3"></tabbardi>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				tabActive: 0,
				tabList: [{
					name: "聊天消息"
				}, {
					name: "互动消息"
				}, {
					name: "系统通知",
					badge: {
						value: 0,
					}
				}],
				user_id: uni.getStorageSync("user_id"),
				pubUrl: "",
				page: 1,
				list: {
					data: []
				},
				user_id: "",
				chatList: [],
				noticepage:1,
				noticelist: {
					data: []
				},
			}
		},
		onLoad() {
			uni.hideTabBar()
			this.pubUrl = this.imgUrl
			uni.$on("updatechatFnList", data => {
				console.log("触发方法了会话列表哈哈哈");
				this.init()
			})
		},
		onShow() {
			this.tabActive = 0
			this.user_id = uni.getStorageSync("user_id")
			if (httpRequest.checkIsLogin()) {
				this.init()
				this.getinfo()
				this.getsystemlist()
			} else {
				this.chatList = []
				this.list = {
					data: []
				}
				this.noticelist = {
					data: []
				}
			}
		},
		onUnload() {
			uni.$off('updatechatFnList')
		},
		methods: {
			getinfo() {
				httpRequest.request('/api/user/index', 'GET', {}).then(res => {
					this.tabList[2].badge.value = res.data.noread_message_num
				})
			},
			init() {
				httpRequest.requestChat('/api/user/getChatList', 'GET', {
					user_id: uni.getStorageSync("user_id"),
					sendtype: 1
				}).then(res => {
					this.chatList = res.data
				})
			},
			getinteract(isPage, page) { //互动消息
				let _this = this;
				httpRequest.request('/api/community/getinteract', 'GET', {
					page: page || 1,
				}, false, false, true).then(res => {
					if (res.code == 1) {
						let resList = res.data;
						let dataList = _this.list;
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							this.list = res.data
							_this.list.data = resList.data;
						}
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			getsystemlist(isPage, page) {
				let _this = this;
				httpRequest.request('/api/user/noticeList', 'GET', {
					page: page || 1,
					limit: 12
				}, false, false, true).then(res => {
					if (res.code == 1) {
						let resList = res.data;
						let dataList = _this.noticelist;
						if (isPage == true) {
							_this.noticelist.data = dataList.data.concat(resList.data);
						} else {
							this.noticelist = res.data
							_this.noticelist.data = resList.data;
						}
					}
				})
			},
			// 进入聊天
			enterchat(item) { //进入聊天
				let data = {
					firend_id: item.firend.id,
					friend_name: item.firend.nickname,
					firend_avatar_text: item.firend.avatar
				}
				uni.navigateTo({
					url: "/pages/chat/chat?msg=" + encodeURIComponent(JSON.stringify(data))
				})
			},
			change(e) {
				this.tabActive = e.index
				if (this.tabActive == 1) {
					this.getinteract()
				} else if (this.tabActive == 2) {
					this.$store.dispatch("getNoticeList", {
						page: 1
					})
					// console.log(this.noticelist.data, "消息内部");
					// console.log(this.$store.state.chat.noticelist.data);
					// this.noticelist=this.$store.state.chat.noticelist
				}
			},
			onReachBottom() {
				if(this.tabActive == 0) return false
				let page = this.page;
				let list = this.list
				if (this.tabActive == 2) {
					page = this.noticepage
					list = this.noticelist
				}
				if (list.last_page <= page) {
					return false;
				}
				page++;
				if (this.tabActive == 1) {
					this.page = page;
				} else {
					this.noticepage = page;
				}
				if (this.tabActive == 1) {
					this.getinteract(true, page);
				} else {
					this.getsystemlist(true, page)
				}
			},
			goInfo(item) { //展示、打赏抢答详情
				if (item.community.type == 1) {
					uni.navigateTo({
						url: `/pages_index/videoInfo?id=${item.community_id}`
					})
				} else if (item.community.type == 2) {
					uni.navigateTo({
						url: `/pages_index/forumInfo?type=1&id=${item.community_id}`
					})
				} else {
					uni.navigateTo({
						url: `/pages_index/forumInfo?type=2&id=${item.community_id}`
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.yihang {
		width: 300rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.content {
		padding-bottom: 150rpx;
	}

	::v-deep.u-tabs__wrapper__nav {
		background-color: #ffffff !important;
	}

	::v-deep.u-tabs__wrapper__nav__item {
		flex: 1 !important;
	}

	// 聊天消息
	.chatBox {
		background-color: #FAFAFA;
		padding: 30rpx 40rpx 0;
	}

	.chatItemMessage {
		display: flex;
		margin-bottom: 40rpx;

		.imageBox {
			width: 114rpx;
			min-width: 114rpx;
			height: 114rpx;
			border-radius: 50%;
			overflow: hidden;
			margin-right: 40rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.right {
			flex: 1;
		}

		.nameTime {
			display: flex;
			justify-content: space-between;

			.name {
				font-size: 32rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #000000;
				margin-right: 10rpx;
			}

			.number {
				width: 34rpx;
				height: 34rpx;
				line-height: 34rpx;
				text-align: center;
				background: #FF526A;
				border-radius: 50%;
				font-size: 20rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
			}

			.time {
				font-size: 24rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #666666;
			}
		}

		.bottom {
			font-size: 26rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #999999;
			margin-top: 20rpx;
		}
	}

	// 互动消息
	.interactionBox {
		background-color: rgba(250, 250, 250, 0.69);
		padding: 30rpx;
	}

	.interactionItem {
		display: flex;
		justify-content: space-between;
		margin-bottom: 40rpx;
		padding: 30rpx;
		background-color: #ffffff;

		.left {
			display: flex;
		}

		.imageBox {
			width: 114rpx;
			height: 114rpx;
			min-width: 114rpx;
			border-radius: 50%;
			overflow: hidden;
			margin-right: 40rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.name {
			font-size: 30rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #F3271C;
		}

		.reply {
			font-size: 24rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #000000;
		}

		.time {
			font-size: 24rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #333333;
		}

		.right {
			width: 114rpx;
			height: 114rpx;
			border-radius: 15rpx 15rpx 15rpx 15rpx;
			overflow: hidden;
			display: flex;
			align-items: center;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.sanhang {
			width: 114rpx;
			height: 89rpx;
			text-overflow: -o-ellipsis-lastline;
			overflow: hidden;
			text-overflow: ellipsis;
			display: -webkit-box;
			-webkit-line-clamp: 3;
			line-clamp: 3;
			-webkit-box-orient: vertical;

			// word-break: break-all;
			// overflow: hidden;
			// text-overflow: ellipsis;
			// display: -webkit-box;
			// -webkit-line-clamp: 3;
			// -webkit-box-orient: vertical;
		}
	}

	.goodsImg {
		width: 124rpx;
		height: 124rpx;
		border-radius: 14rpx 14rpx 14rpx 14rpx;
		overflow: hidden;

		image {
			width: 100%;
			height: 100%;
		}
	}
</style>